package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.BusfrontIntro;
import com.ruoyi.business.mapper.BusfrontIntroMapper;
import com.ruoyi.business.service.IBusfrontIntroService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.PathVo;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 区域营商环境介绍（前台）Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-04
 */
@Service
public class BusfrontIntroServiceImpl implements IBusfrontIntroService
{
    @Autowired
    private BusfrontIntroMapper busfrontIntroMapper;
    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询区域营商环境介绍（前台）
     *
     * @param id 区域营商环境介绍（前台）主键
     * @return 区域营商环境介绍（前台）
     */
    @Override
    public BusfrontIntro selectBusfrontIntroById(Long id)
    {
        busfrontIntroMapper.addPageViews(id);
        BusfrontIntro busfrontIntro = busfrontIntroMapper.selectBusfrontIntroById(id);
        if (busfrontIntro.getServiceBy()!=null&&!busfrontIntro.getServiceBy().equals("")) {
            SysUser sysUser = busfrontIntroMapper.selectSysUserById(busfrontIntro.getServiceBy());
            busfrontIntro.setPerson(sysUser.getNickName());
            busfrontIntro.setPhone(sysUser.getPhonenumber());
        }
        return busfrontIntro;
    }

    @Override
    public List<BusfrontIntro> selectBusfrontIntroByIds(Long[] ids) {
        return busfrontIntroMapper.selectBusfrontIntroByIds(ids);
    }

    /**
     * 查询区域营商环境介绍（前台）列表
     *
     * @param busfrontIntro 区域营商环境介绍（前台）
     * @return 区域营商环境介绍（前台）
     */
    @Override
    public List<BusfrontIntro> selectBusfrontIntroList(BusfrontIntro busfrontIntro)
    {
        List<BusfrontIntro> busfrontIntros = busfrontIntroMapper.selectBusfrontIntroList(busfrontIntro);
        for (BusfrontIntro intro : busfrontIntros) {
            if (intro.getServiceBy()!=null) {
                SysUser sysUser = busfrontIntroMapper.selectSysUserById(intro.getServiceBy());
                if (sysUser != null) {
                    intro.setPerson(sysUser.getNickName());
                    intro.setPhone(sysUser.getPhonenumber());
                }
            }
        }
        return busfrontIntros;
    }

    @Override
    public List<BusfrontIntro> selectBusfrontIntroListz(BusfrontIntro busfrontIntro)
    {
        Long id = busfrontIntro.getId();
        busfrontIntro.setId(null);
        List<BusfrontIntro> intros = busfrontIntroMapper.selectBusfrontIntroList(busfrontIntro);
        for (BusfrontIntro intro : intros) {
            if (intro.getServiceBy()!=null) {
                SysUser sysUser = busfrontIntroMapper.selectSysUserById(intro.getServiceBy());
                if (sysUser != null) {
                    intro.setPerson(sysUser.getNickName());
                    intro.setPhone(sysUser.getPhonenumber());
                }
            }
        }
        if (id!=null) {
            return intros.stream().filter(o -> !Objects.equals(o.getId(), id)).collect(Collectors.toList());
        }
        return intros;
    }


    @Override
    public List<BusfrontIntro> selectBusfrontIntroPopularList(BusfrontIntro busfrontIntro) {
        List<BusfrontIntro> busfrontIntros = busfrontIntroMapper.selectBusfrontIntroPopularList(busfrontIntro);
        for (BusfrontIntro intro : busfrontIntros) {
            if (intro.getServiceBy()!=null) {
                SysUser sysUser = busfrontIntroMapper.selectSysUserById(intro.getServiceBy());
                if (sysUser != null) {
                    intro.setPerson(sysUser.getNickName());
                    intro.setPhone(sysUser.getPhonenumber());
                }
            }
        }
        return busfrontIntros;
    }

    @Override
    public List<BusfrontIntro> selectBusfrontIntroNewlyAddedList(BusfrontIntro busfrontIntro) {
        List<BusfrontIntro> busfrontIntros = busfrontIntroMapper.selectBusfrontIntroNewlyAddedList(busfrontIntro);
        for (BusfrontIntro intro : busfrontIntros) {
            if (intro.getServiceBy()!=null) {
                SysUser sysUser = busfrontIntroMapper.selectSysUserById(intro.getServiceBy());
                if (sysUser != null) {
                    intro.setPerson(sysUser.getNickName());
                    intro.setPhone(sysUser.getPhonenumber());
                }
            }
        }
        return busfrontIntros;
    }

    /**
     * 新增区域营商环境介绍（前台）
     *
     * @param busfrontIntro 区域营商环境介绍（前台）
     * @return 结果
     */
    @Override
    public int insertBusfrontIntro(BusfrontIntro busfrontIntro)
    {
        busfrontIntro.setState(0L);
        busfrontIntro.setGovernId(SecurityUtils.getUserId());
        busfrontIntro.setCreateTime(DateUtils.getNowDate());
        return busfrontIntroMapper.insertBusfrontIntro(busfrontIntro);
    }

    /**
     * 修改区域营商环境介绍（前台）
     *
     * @param busfrontIntro 区域营商环境介绍（前台）
     * @return 结果
     */
    @Override
    public int updateBusfrontIntro(BusfrontIntro busfrontIntro)
    {
        busfrontIntro.setState(0L);
        busfrontIntro.setUpdateTime(DateUtils.getNowDate());
        return busfrontIntroMapper.updateBusfrontIntro(busfrontIntro);
    }

    /**
     * 批量删除区域营商环境介绍（前台）
     *
     * @param ids 需要删除的区域营商环境介绍（前台）主键
     * @return 结果
     */
    @Override
    public int deleteBusfrontIntroByIds(Long[] ids)
    {
        //根据ids查询需要删除的区域营商内容
        List<BusfrontIntro> busfrontIntros = busfrontIntroMapper.selectBusfrontIntroByIds(ids);
        List<String> pathList = new ArrayList<>();
        for (BusfrontIntro busfrontIntro : busfrontIntros) {
            busfrontIntroMapper.deleteBusfrontRecomById(busfrontIntro.getId());
            pathList.add(busfrontIntro.getCover());
        }
        remoteFileService.delete(new PathVo(pathList), SecurityConstants.INNER);
        return busfrontIntroMapper.deleteBusfrontIntroByIds(ids);
    }

    /**
     * 删除区域营商环境介绍（前台）信息
     *
     * @param id 区域营商环境介绍（前台）主键
     * @return 结果
     */
    @Override
    public int deleteBusfrontIntroById(Long id)
    {
        busfrontIntroMapper.deleteBusfrontRecomById(id);
        return busfrontIntroMapper.deleteBusfrontIntroById(id);
    }
}
